<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Libs\Common;

final class CommArray
{
    /**
     * @param array $array
     * @return bool
     * isEmpty
     */
    public static function isEmpty(array $array):bool
    {
        return count($array) < 1;
    }

    /**
     * @param $array
     * @return bool
     * isForeach
     */
    public static function isForeach($array):bool
    {
        return is_array($array) && !self::isEmpty($array);
    }

    /**
     * @param array $array
     * @param string $search
     * @param bool $isKey
     * @return int
     * getIndexNum
     */
    public static function getIndexNum(array $array, string $search, bool $isKey = false):int
    {
        if(!self::isEmpty($array)){
            $index = 0;
            foreach($array as $key => $val){
                if($isKey){
                    if($key === $search) return $index;
                }else{
                    if($val === $search) return $index;
                }
                ++$index;
            }
        }
        return -1;
    }

    /**
     * @param $object
     * @return array
     * objectToArray
     */
    public static function objectToArray($object):array
    {
        return CommJson::decodeArray(CommJson::encode($object));
    }
}